package com.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.pojo.*;
import com.system.server.*;
import exception.*;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年04月14日14:15
 * @Description: <描述>
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private TypeService typeService;
    @Resource
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;

    @ModelAttribute("roleName")
    public String userType(HttpSession session) {
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");

        Role role = typeService.findRoleByUserLoginName(userLogin.getUsername());
        String roleName = role.getRoleName();

        return roleName;
    }
    @ModelAttribute("teachers")
    public List<Teacher> showTeacher() {
        List<Teacher> teachers = teacherService.findTeacherNo();
        return teachers;
    }
    @ModelAttribute("colleges")
    public List<College> showCollege() {
        List<College> colleges = adminService.findCollege();
        return colleges;
    }
    @RequestMapping("/findAll")
    public String showCourse(Integer pageNum, Model model) {
        if (ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        //分页插件
        PageHelper.startPage(pageNum, 5);

        List<Course> lists = courseService.findAll();

        PageInfo<Course> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo", pageInfo);
        return "/admin/showCourse";
    }
    @RequestMapping("/searchCourse")
    public String searchCourse(String findByName,Integer pageNum,Model model) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);

        List<Course> lists = courseService.findCourseByName(findByName);

        PageInfo<Course> pageInfo = new PageInfo<>(lists);
        if(pageNum > pageInfo.getPages()) {
            pageNum = pageInfo.getPages();
            PageHelper.startPage(pageNum,5);
            List<Course> lists1 = courseService.findCourseByName(findByName);
            PageInfo<Course> pageInfo1 = new PageInfo<>(lists1);

            model.addAttribute("pageInfo",pageInfo1);
            //数据回显
            model.addAttribute("conditionName",findByName);

            return "/admin/showCourse";
        }
        model.addAttribute("pageInfo",pageInfo);
        //数据回显
        model.addAttribute("conditionName",findByName);

        return "/admin/showCourse";
    }
    @RequestMapping("/addCoursePage")
    public String addCoursePage() {
        return "admin/addCourse";
    }
    @RequestMapping("/checkCourseID")
    @ResponseBody
    public Map<String,Object> checkCourseID(Integer courseID) {
        Map<String, Object> map = new HashMap<>();
        boolean flag = adminService.checkCourseID(courseID);
        if(flag) {
            map.put("valid",true);
        } else {
            map.put("valid",false);
            map.put("message","该课程("+courseID+")已经存在");
        }
        return map;
    }
    @RequestMapping("/addCourse")
    public String addCourse(Course course,Model model) {
        try {
            adminService.addCourse(course);
            model.addAttribute("successMsg","添加成功");
        } catch (AddCourseError e) {
            model.addAttribute("errorMsg",e.getMessage());
        }
        return "redirect:/admin/findAll";
    }
    @RequestMapping("/editCoursePage")
    public String editCoursePage(Integer id,Model model) {
        Course course = adminService.findCourseById(id);
        model.addAttribute("course",course);
        return "/admin/editCourse";
    }
    @RequestMapping("/editCourse")
    public String editCourse(Course course) {
        adminService.modifyCourse(course);

        return "redirect:/admin/findAll";
    }
    @RequestMapping("/deleteCourse")
    public String deleteCourse(Integer courseID) {
        adminService.removeCourse(courseID);

        return "redirect:/admin/findAll";
    }

    @RequestMapping("/findAll_student")
    public String findAll_student(Integer pageNum,Model model) {
        if(ObjectUtils.isEmpty(pageNum)) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);

        List<Student> lists = studentService.findAll();

        PageInfo<Student> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);

        return "/admin/showStudent";
    }
    @RequestMapping("/searchStudent")
    public String searchStudent(Integer pageNum,String findByName,Model model) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);
        if(ObjectUtils.isEmpty(findByName)) {
            List<Student> lists = studentService.findAll();
            PageInfo<Student> pageInfo = new PageInfo<>(lists);
            model.addAttribute("pageInfo",pageInfo);
            //数据回显
            model.addAttribute("conditionName",findByName);

            return "/admin/showStudent";
        } else {
            PageHelper.startPage(pageNum,5);
            List<Student> lists = studentService.findStudentByName(findByName);
            PageInfo<Student> pageInfo = new PageInfo<>(lists);
            if(pageNum > pageInfo.getPages()) {
                pageNum = pageInfo.getPages();
                PageHelper.startPage(pageNum,5);

                List<Student> lists1 = studentService.findStudentByName(findByName);
                PageInfo<Student> pageInfo1 = new PageInfo<>(lists1);

                model.addAttribute("pageInfo",pageInfo1);
                //数据回显
                model.addAttribute("conditionName",findByName);

                return "/admin/showStudent";
            }
            model.addAttribute("pageInfo",pageInfo);
            //数据回显
            model.addAttribute("conditionName",findByName);
            return "/admin/showStudent";
        }
    }
    @RequestMapping("/addStudentPage")
    public String addStudentPage() {
        return "/admin/addStudent";
    }
    @RequestMapping("/addStudent")
    public String addStudent(StudentVO student,Model model) {
        try {
            studentService.addStudent(student);
        } catch (AddStudentError e) {
            model.addAttribute("ErrorMsg",e.getMessage());
        }
        return "redirect:/admin/findAll";
    }
    @RequestMapping("/checkStudentID")
    @ResponseBody
    public Map<String,Object> checkStudentID(Integer userID) {
        Map<String,Object> map = new HashMap<>();
        Boolean flag = studentService.checkStudentID(userID);
        if(flag) {
            map.put("valid",true);
        } else {
            map.put("valid",false);
            map.put("message","该学生("+userID+")已经添加");
        }
        return map;
    }
    @RequestMapping("/editStudentPage")
    public String editStudentPage(Integer id,Model model) {
        Student student = studentService.findStudentByID(id);
        model.addAttribute("student",student);

        return "/admin/editStudent";
    }
    @RequestMapping("/editStudent")
    public String editStudent(StudentVO studentVO) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        Student student = new Student();

        College college = new College();
        college.setCollegeID(studentVO.getCollegeID());

        student.setCollege(college);
        PropertyUtils.copyProperties(student,studentVO);

        studentService.modifyStudent(student);

        return "redirect:/admin/findAll";
    }
    @RequestMapping("/removeStudent")
    public String removeStudent(Integer id) {
        studentService.removeStudent(id);

        return "redirect:/admin/findAll";
    }
    @RequestMapping("/findAll_teacher")
    public String findAll_teacher(Integer pageNum,Model model) {
        if(StringUtils.isEmpty(pageNum)) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);

        List<Teacher> lists = teacherService.findAll();

        PageInfo<Teacher> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);
        return "admin/showTeacher";
    }
    @RequestMapping("/selectTeacher")
    public String selectTeacher(String findByName, Model model,Integer pageNum) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);
        if(ObjectUtils.isEmpty(findByName)) {
            List<Teacher> lists = teacherService.findAll();
            PageInfo<Teacher> pageInfo = new PageInfo<>(lists);
            model.addAttribute("pageInfo",pageInfo);
            //数据回显
            model.addAttribute("conditionName",findByName);

            return "/admin/showTeacher";
        } else {
            List<Teacher> lists = teacherService.findTeacherByName(findByName);

            PageInfo<Teacher> pageInfo = new PageInfo<>(lists);

            model.addAttribute("pageInfo",pageInfo);
            //数据回显
            model.addAttribute("conditionName",findByName);

            return "/admin/showTeacher";
        }
    }
    @RequestMapping("/addTeacherPage")
    public String addTeacherPage() {
        return "/admin/addTeacher";
    }
    @RequestMapping("/checkTeacherID")
    @ResponseBody
    public Map<String,Object> checkTeacherID(Integer userID) {
        Map<String,Object> map = new HashMap<>();
        Boolean flag = teacherService.checkStudentID(userID);
        if(flag) {
            map.put("valid",true);
        } else {
            map.put("valid",false);
            map.put("message","该老师("+userID+")已经添加");
        }
        return map;
    }
    @RequestMapping("/addTeacher")
    public String addTeacher(TeacherVO teacherVO) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Teacher teacher = new Teacher();

        College college = new College();
        college.setCollegeID(teacherVO.getCollegeID());
        teacher.setCollege(college);

        PropertyUtils.copyProperties(teacher,teacherVO);

        teacherService.addTeacher(teacher);

        return "redirect:/admin/findAll";
    }
    @RequestMapping("/editTeacherPage")
    public String editTeacherPage(Integer userID,Model model) {
        TeacherVO teacher = teacherService.findTeacherById(userID);
        model.addAttribute("teacher",teacher);
        return "/admin/editTeacher";
    }
    @RequestMapping("/editTeacher")
    public String editTeacher(TeacherVO teacherVO) {
        teacherService.modifyTeacher(teacherVO);
        return "redirect:/admin/findAll";
    }
    @RequestMapping("/removeTeacher")
    public String removeTeacher(Integer userID,Model model) {
        try {
            teacherService.removeTeacher(userID);
            model.addAttribute("successMsg","删除成功");
        } catch (DeleteTeacherError e) {
            model.addAttribute("errorMsg",e.getMessage());
        }
        return "forward:/admin/findAll_teacher";
    }
    @RequestMapping("/userPasswordRestPage")
    public String userPasswordRestPage() {
        return "admin/userPasswordRestPage";
    }
    @RequestMapping("/userPasswordRest")
    public String userPasswordRest(String userName,String newPassword,String againPassword,Model model) {
        try {
            teacherService.modifyPassword(userName,newPassword,againPassword);
            model.addAttribute("successMsg","修改密码成功");
        } catch (exception.passwordDiffError e) {
            model.addAttribute("errorMsg",e.getMessage());
        } catch (exception.modifyPasswordError e) {
            model.addAttribute("errorMsg",e.getMessage());
        } catch (exception.userLoginNotFound e) {
            model.addAttribute("errorMsg",e.getMessage());
        } catch (exception.passwordNotEmptyError e) {
            model.addAttribute("errorMsg",e.getMessage());
        }
        return "admin/userPasswordRestPage";
    }

}
